package com.ruoyi.project.tool.swagger.UserController;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Ide;
import com.ruoyi.framework.aspectj.lang.annotation.SignatureValidation;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysMobileUser;
import com.ruoyi.project.system.domain.SysReceiveCoupon;
import com.ruoyi.project.system.service.ISysCouponService;

import com.ruoyi.project.system.service.ISysReceiveCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/mobileUser/receive/")
public class UserReceiveController extends BaseController {

    @Autowired
    private ISysCouponService sysCouponService;

    @Autowired
    private ISysReceiveCouponService sysReceiveCouponService;

    /**
     * 未登录显示优惠券
     */
    @SignatureValidation
    @GetMapping("getNotLoginReceiveCoupon")
    public AjaxResult getNotLoginReceiveCoupon() {
        return AjaxResult.success(sysCouponService.selectCouponUserd());
    }
    /**
     * 查询未领优惠券
     * */
    @SignatureValidation
    @GetMapping("getLoginReceiveCoupon")
    public AjaxResult getLoginReceiveCoupon(){
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        return AjaxResult.success(sysReceiveCouponService.selectUserCoupon(mobileUserId));
    }
    /**
     * 未使用优惠券 展示
     * */
    @SignatureValidation
    @GetMapping("getNotCoupon")
    public TableDataInfo getNotCoupon(SysReceiveCoupon sysReceiveCoupon) {
        startPage();
        Long mobileUserId = SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId();
        if (null == sysReceiveCoupon.getNum()) {
            return getDataTable(sysReceiveCouponService.notCoupon(mobileUserId));
        }
        if (sysReceiveCoupon.getNum() == 1) {
            //已使用的优惠券
            return getDataTable(sysReceiveCouponService.yesCoupon(mobileUserId));
        }
        if (sysReceiveCoupon.getNum() == 2) {
            //已过期的优惠券
            return getDataTable(sysReceiveCouponService.notUseCoupon(mobileUserId));
        }
        // 未使用的优惠券
        return getDataTable(sysReceiveCouponService.notCoupon(mobileUserId));
    }

    /**
     * 领取优惠券
     * */
    @PostMapping("receiveCoupon")
    @Ide
    @Transactional
    public AjaxResult receiveCoupon(@RequestBody SysReceiveCoupon sysReceiveCoupon){
        sysReceiveCoupon.setMobileUserId(SecurityUtils.getLoginUser().getSysMobileUser().getMobileUserId());
        sysReceiveCoupon.setUsed("0");
        sysReceiveCouponService.insertSysReceiveCoupon(sysReceiveCoupon);
        sysCouponService.updateCoupon(sysReceiveCoupon.getCouponId());
        return AjaxResult.success("领取成功");
    }
}